package com.example.controller;

import com.example.common.utils.R;
import com.example.system.dto.JobDTO;
import com.example.system.entity.QuartzJob;
import com.example.system.service.QuartzJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 王鹤蒲
 * @Date 2023 11 20
 */


@RestController
@Api()
@RequestMapping("/job")
public class QuartzJobController {

    @Autowired
    private QuartzJobService quartzJobService;

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/addJob",method= RequestMethod.POST)
    @ResponseBody
    public R addJob(@RequestBody QuartzJob quartzJob) throws SchedulerException, ClassNotFoundException {
        return quartzJobService.addJob(quartzJob);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/deleteJob",method= RequestMethod.DELETE)
    @ResponseBody
    public R deleteJob(@RequestParam(value = "jobName",required = true)String jobName, @RequestParam(value = "jobGroupName",required = true) String jobGroupName) throws SchedulerException {
        return quartzJobService.deleteJob(jobName,jobGroupName);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/deleteJobs",method= RequestMethod.POST)
    @ResponseBody
    public R deleteBatch(@RequestBody List<JobDTO> jobDTOList) throws SchedulerException {
        return quartzJobService.deleteBatch(jobDTOList);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/pauseJob",method= RequestMethod.POST)
    @ResponseBody
    public R pauseJob(@RequestParam(value = "jobName",required = true)String jobName, @RequestParam(value = "jobGroupName",required = true) String jobGroupName) throws SchedulerException {
        return quartzJobService.pauseJob(jobName,jobGroupName);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/continueJob",method= RequestMethod.POST)
    @ResponseBody
    public R continueJob(@RequestParam(value = "jobName",required = true)String jobName, @RequestParam(value = "jobGroupName",required = true) String jobGroupName)throws SchedulerException{
        return quartzJobService.continueJob(jobName,jobGroupName);
    }


    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/getJob",method= RequestMethod.GET)
    @ResponseBody
    public R getQuartzJob(@RequestParam(value = "jobName",required = true)String jobName, @RequestParam(value = "jobGroupName",required = true) String jobGroupName) throws SchedulerException {
        return R.success(quartzJobService.getQuartzJob(jobName,jobGroupName));
    }


    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/getQuartzJobList",method= RequestMethod.GET)
    @ResponseBody
    public R getQuartzJobList(JobDTO jobDTO, int currentPage, int pageSize) throws SchedulerException {
        return quartzJobService.getQuartzJobList(jobDTO,currentPage,pageSize);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/updateJob",method= RequestMethod.GET)
    @ResponseBody
    public R updateJob(QuartzJob quartzJob,String quartzJobName,String quartzJobGroupName) throws SchedulerException, ClassNotFoundException {
        return quartzJobService.updateJob(quartzJob,quartzJobName,quartzJobGroupName);
    }

    @ApiOperation(value = "", notes = "")
    @RequestMapping(value="/updateJobStatus",method= RequestMethod.GET)
    @ResponseBody
    public R updateJobStatus(String jobName, String jobGroupName, Boolean status){
        return quartzJobService.updateJobStatus(jobName,jobGroupName,status);
    }
}
